﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace WorldMapNew
{
    /// <summary>
    /// 地图块数据结构
    /// </summary>
    public class MapChunckData : ScriptableObject
    {
        public int chunckSize;                                      //地图块尺寸 单位为米
        public List<MapChunckLayer> mapLayers;                      //地图上物件分层存储数据列表
        public List<TileAlliancePointRoad> alliancePointRoads; // 街区点 二车道
        public List<TileAlliancePointCross> alliancePointCross; // 街区点 衔接车道
        public List<TilePointAdornData> pointAdornDatas; // 出生点 装饰物数据

        public MapChunckData()
        {
            mapLayers = new List<MapChunckLayer>();
        }

        /// <summary>
        /// 添加地图的一层数据
        /// </summary>
        /// <param name="layer"></param>
        public void AddMapLayer(MapChunckLayer layer)
        {
            mapLayers.Add(layer);
        }

        public void AddChunckePointRoad(TileAlliancePointRoad road)
        {
            if (alliancePointRoads == null)
            {
                alliancePointRoads = new List<TileAlliancePointRoad>();
            }
            alliancePointRoads.Add(road);
        }

        public void AddChunckePointCross(TileAlliancePointCross cross)
        {
            if (alliancePointCross == null)
            {
                alliancePointCross = new List<TileAlliancePointCross>();
            }
            alliancePointCross.Add(cross);
        }

        public void AddChunckePointAdorn(TilePointAdornData adorn)
        {
            if (pointAdornDatas == null)
            {
                pointAdornDatas = new List<TilePointAdornData>();
            }
            pointAdornDatas.Add(adorn);
        }
    }

    /// <summary>
    /// 地图块 删除数据结构
    /// </summary>
    [Serializable]
    public class MapChunckDeleateData
    {
        public List<int> deleateIdx;             //对应删除装饰物的idx

        public MapChunckDeleateData()
        {
            deleateIdx = new List<int>();
        }
    }


    /// <summary>
    /// 地图块 街区点 二车道的数据结构
    /// </summary>
    [Serializable]
    public class TileAlliancePointRoad
    {
        public Vector3 position;
        public int resID;
        public int localIdx;// 10x10 在 30x30中一维坐标 0 ~ 8

        public TileAlliancePointRoad(Vector3 p, int resID, int idx)
        {
            this.position = p;
            this.position.x = (float)Math.Round(position.x, 2, MidpointRounding.AwayFromZero);
            this.position.y = (float)Math.Round(position.y, 2, MidpointRounding.AwayFromZero);
            this.position.z = (float)Math.Round(position.z, 2, MidpointRounding.AwayFromZero);
            this.resID = resID;
            this.localIdx = idx;
        }

        public TileAlliancePointRoad()
        {
        }
        public TileAlliancePointRoad(TileAlliancePointRoad other)
        {
            this.position = other.position;
            this.resID = other.resID;
            this.localIdx = other.localIdx;
        }
    }

    /// <summary>
    /// 地图块 街区点 衔接车道的数据结构
    /// </summary>
    [Serializable]
    public class TileAlliancePointCross
    {
        public Vector3 position;
        public int resID;
        public int localIdx;// 10x10 在 30x30中一维坐标
        public int angleIdx;// 衔接道路方向idx 顺时针 idx * 90 0 ~ 3

        public TileAlliancePointCross(Vector3 p, int resID, int idx, int angleIdx)
        {
            this.position = p;
            this.position.x = (float)Math.Round(position.x, 2, MidpointRounding.AwayFromZero);
            this.position.y = (float)Math.Round(position.y, 2, MidpointRounding.AwayFromZero);
            this.position.z = (float)Math.Round(position.z, 2, MidpointRounding.AwayFromZero);
            this.resID = resID;
            this.localIdx = idx;
            this.angleIdx = angleIdx;
        }

        public TileAlliancePointCross(TileAlliancePointCross other)
        {
            this.position = other.position;
            this.resID = other.resID;
            this.localIdx = other.localIdx;
            this.angleIdx = other.angleIdx;
        }
    }

    /// <summary>
    /// 地图块 出生点 装饰物数据结构
    /// </summary>
    [Serializable]
    public class TilePointAdornData
    {
        public int resId;
        public int type;// [1,4] 代表方向 顺时针 [5,8] 代表边 顺时针 上右下左 10 代表三级装饰物 9 代表衔接道路 11 代表空置装饰物 -1无效值 0无 但是表示两出生点相连
        public List<int> pointIdxs;// 当list中所有出生点被占据 才会显示
        public Vector3 position;
        public Vector3 rotation;
        public int uid;// 标识id （其实就是记录的时候的数组下标）

        public List<int> lowLevelIds;// 当前装饰物出现 下一级装饰物不出现的uid集合
        public List<int> hightLevelIds;// 上一级装饰物uid集合不出现 当前装饰物才出现 

        public TilePointAdornData()
        {

        }

        public TilePointAdornData(int resId)
        {
            this.resId = resId;
        }

        public TilePointAdornData(TileAlliancePointRoad roadData)
        {
            resId = roadData.resID;
            position = roadData.position;
            uid = roadData.localIdx;
        }

        public TilePointAdornData(TileAlliancePointCross crossData)
        {
            resId = crossData.resID;
            position = crossData.position;
            uid = crossData.localIdx;
        }

        public void AddPointIdx(int idx)
        {
            if (pointIdxs == null)
            {
                pointIdxs = new List<int>();
            }
            pointIdxs.Add(idx);
        }

        public void AddLow(int uid)
        {
            if (lowLevelIds == null) lowLevelIds = new List<int>();
            if (!lowLevelIds.Contains(uid)) lowLevelIds.Add(uid);
        }

        public void AddHight(int uid)
        {
            if (hightLevelIds == null) hightLevelIds = new List<int>();
            if (!hightLevelIds.Contains(uid)) hightLevelIds.Add(uid);
        }
    }
}